﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class PartnerBLL
  {
    //Get all
    public List<Partner> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Partners.OrderBy(p => p.Sort).ToList();
      }
    }

    public List<Partner> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Partners.Where(p => p.isEnable == isEnable).OrderBy(p => p.Sort).ToList();
      }
    }

    //Get by id

    public Partner GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Partners.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public Partner GetById(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Partners.Where(p => p.Id == id && p.isEnable == isEnable).FirstOrDefault();
      }
    }

    //Enable

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner obj = data.Partners.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;

        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner obj = data.Partners.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;

        data.SubmitChanges();
      }
    }

    //Get max sort

    public byte GetMaxSort()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner obj = data.Partners.OrderByDescending(p => p.Sort).FirstOrDefault();

        if (obj == null) return 1;
        else return (byte)(obj.Sort + 1);
      }
    }

    //Swap sort

    public void SwapSort(int idFirst, int idSecond)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner objFirst = data.Partners.Where(p => p.Id == idFirst).FirstOrDefault();
        Partner objSecond = data.Partners.Where(p => p.Id == idSecond).FirstOrDefault();

        byte tg = objFirst.Sort;
        objFirst.Sort = objSecond.Sort;
        objSecond.Sort = tg;

        data.SubmitChanges();
      }
    }

    //Add

    public void Add(Partner obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Partners.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    //Update 

    public void Update(Partner obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner partner = data.Partners.Where(p => p.Id == obj.Id).FirstOrDefault();

        partner.Content = obj.Content;
        partner.Description = obj.Description;
        partner.Image = obj.Image;
        partner.isEnable = obj.isEnable;
        partner.Link = obj.Link;
        partner.Name = obj.Name;
        partner.Sort = obj.Sort;

        data.SubmitChanges();
      }
    }

    //Delete

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Partner obj = data.Partners.Where(p => p.Id == id).FirstOrDefault();
        if(obj!= null)
          data.Partners.DeleteOnSubmit(obj);

        data.SubmitChanges();
      }
    }
  }
}
